package com.cljh.service.impl;

import com.cljh.bean.Path;
import com.cljh.bean.Role;
import com.cljh.controller.page.PageBean;
import com.cljh.mapper.MenuMapper;
import com.cljh.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    MenuMapper menuMapper;
    @Override
    public boolean save(Path role) {
        return menuMapper.save(role)>0;
    }

    @Override
    public boolean delete(Integer id) {
        return menuMapper.delete(id)>0;
    }

    @Override
    public boolean Upd(Path role) {
        return menuMapper.Upd(role)>0;
    }

    @Override
    public Path selectById(Integer id) {
        return menuMapper.selectById(id);
    }

    @Override
    public PageBean<Path> selectAll(Integer current, Integer size, String role) {
        if (role!=null&&role.length()>0){
            role="%"+role+"%";
        }
        int begin=(current-1)*size;
        List<Path> roles = menuMapper.selectAll(begin, size, role);
        int i = menuMapper.selectCount(role);
        PageBean<Path> pageBean = new PageBean<>();
        pageBean.setTotalCount(i);
        pageBean.setRows(roles);


        return pageBean;
    }

    @Override
    public boolean deleteIds(List<Integer> ids) {
        return menuMapper.deleteIds(ids)>0;
    }

    @Override
    public List<Path> selectAllPath() {
        List<Path> role = menuMapper.selectAllPath();
        //找出pid为null的一级菜单
        List<Path> collect = role.stream().filter(menu -> menu.getPathId() == null).collect(Collectors.toList());
        //找出一级菜单
        for (Path p : collect) {
            p.setChildren(role.stream().filter(m -> p.getId().equals(m.getPathId()) ).collect(Collectors.toList()));
        }

        return collect;
    }
}
